******************************************************************
* Paul
* Liquidity Shocks and Demand for FinTech Loans
* Input: Aggregated industry data on FinTech loan demand
* Output: Regression results examining the relationship 
*         between liquidity shocks and FinTech loan demand
******************************************************************

/* Step 1: Import Data */
clear all

use "$output\ifp_scrapped_aggregated_industries_modified.dta", clear

/* Remove duplicate firms */
duplicates drop siren, force 
keep siren 
save "$output\list_siren_input.dta", replace 

/* Import customer default probability data */
import delimited "$input\CIPE_victim_probability_relax_exogenous.csv", clear

/* Keep only firms that appear in the unmatched sample */
merge m:1 siren using "$output\list_siren_input.dta", keep(3) nogen

/* Save the restricted dataset */
save "$output\CIPE_victim_probability_relax_exogenous_restricted.dta", replace

/* Reload the dataset */
use "$output\CIPE_victim_probability_relax_exogenous_restricted.dta", replace

/* Step 2: Define Time Variables */
gen time = date(date,"YMD")  // Convert date to Stata format
gen qdate = qofd(time)       // Convert date to quarterly format
format time %td
format qdate %tq
tsset siren qdate, quarterly  // Define time-series structure

/* Step 3: Define CIPE-related Variables */
gen cipeend = cipeendogenous > 0  
replace cipeend = 0 if cipeendogenous == .
gen cipeexo = cipeexogenous > 0  
replace cipeexo = 0 if cipeexogenous == .
gen cipe = cipeexo | cipeend  // Indicator for either type of CIPE event

/* Assign maximum P2P flag per firm */
bys siren: egen p2p_max = max(p2p)
replace p2p = p2p_max

/* Step 4: Industry Classification Adjustments */
tab industry p2p
replace industry = "public" if inlist(industry, "P", "Q", "R", "S")  // Public sector
replace industry = "DE" if inlist(industry, "D", "E")  // Merge D and E
replace industry = "FL" if inlist(industry, "F", "L")  // Merge F and L

/* Remove irrelevant industries */
drop if inlist(industry, "A", "K", "O", "B", "DE", "0000Z")

/* Generate grouped industry variable */
egen industry10 = group(industry)

sort siren qdate

/* Keep observations within the desired date range */
keep if qdate < qofd(date("20200101","YMD"))
keep if qdate > qofd(date("20131231","YMD"))

tsset siren qdate, quarterly  // Re-confirm time-series settings

/* Step 5: Open Log File */
capture log close
log using "$logs\CIPE_proba_motives_mar28.log", replace

/* Ensure the dead variable is well-defined */
replace dead = 0 if dead == .

/* Step 6: Generate Lagged CIPE Variables */
gen Ncipe = cipe  // Current period CIPE event
gen Lcipe = L.cipe  // One-period lag
gen L2cipe = cipe == 0 & L.cipe == 0 & (L2.cipe + L3.cipe + L4.cipe >= 1)
replace L2cipe = . if L4.cipe == .

/* Step 7: Run Regressions */
capture estimates drop *

/* Regression 1: FinTech loan demand as a function of customer defaults */
eststo reg1: reghdfe newloan i1.p2p##Ncipe, a(i.industry10#i.qdate i.siren) cl(siren)
estadd local firm "Y", replace
estadd local indqtr "Y", replace

/* Regression 2: Using one-period lagged customer defaults */
eststo regL: reghdfe newloan i1.p2p##Lcipe, a(i.industry10#i.qdate i.siren) cl(siren)
estadd local firm "Y", replace
estadd local indqtr "Y", replace

/* Regression 3: Using lagged customer defaults from before q-2 */
eststo regL2: reghdfe newloan i1.p2p##L2cipe, a(i.industry10#i.qdate i.siren) cl(siren)
estadd local firm "Y", replace
estadd local indqtr "Y", replace

/* Step 8: Additional Regressions Using Alternative CIPE Definitions */
preserve

/* Regression 4: Using only exogenous CIPE events */
rename Ncipe Ncipe_old
rename cipeexo Ncipe
eststo reg4: reghdfe newloan i1.p2p##Ncipe, a(i.industry10#i.qdate i.siren) cl(siren)
estadd local firm "Y", replace
estadd local indqtr "Y", replace

/* Regression 5: Using only endogenous CIPE events */
rename Ncipe Ncipeexo
rename cipeend Ncipe
eststo reg5: reghdfe newloan i1.p2p##Ncipe, a(i.industry10#i.qdate i.siren) cl(siren)
estadd local firm "Y", replace
estadd local indqtr "Y", replace

restore

/* Step 9: Export Results */
esttab reg1 regL regL2 reg4 reg5 using "$tables\Table_H1_Liquidity_shocks_and_demand_for_FinTech_loans.tex", replace ///
noomitted nobase  fragment gap booktabs label nonote noobs nolines nonumber nomtitle ///
 eqlabel(none) ///
b(%9.3f)  se(%9.3f)  star(* 0.10 ** 0.05 *** 0.01)  ///
mgroups("All motives" "Customer illiquidity" "Others", ///
pattern (1 0  0 1 1)  ///
prefix(\multicolumn{@span}{c}{)  ///
suffix(}) ///
span erepeat(\cmidrule(lr){@span})) ///
keep(1.p2p#1.Ncipe  1.Ncipe 1.p2p#1.Lcipe 1.Lcipe 1.p2p#1.L2cipe 1.L2cipe) ///
order(1.p2p#1.Ncipe  1.Ncipe 1.p2p#1.Lcipe 1.Lcipe 1.p2p#1.L2cipe 1.L2cipe) ///
varlabel(1.Ncipe "Customer default$_{q}$" 1.p2p#1.Ncipe `"FinTech $\times$ Customer default$_{q}$"' ///
         1.Lcipe "Customer default$_{q-1}$" 1.p2p#1.Lcipe `"FinTech $\times$ Customer default$_{q-1}$"' ///
         1.L2cipe "Customer default$_{\textit{Before q-2}$" 1.p2p#1.L2cipe `"FinTech $\times$ Customer default$_{\textit{Before q-2}}$"') ///
stats(firm indqtr N r2, label(`"Firm FE"' `"Industry $\times$ Quarter  FE"' `"N"' `"R-sq"') layout("\multicolumn{1}c{@}")  fmt(%9.0fc %9.0fc %9.0fc %9.2f )) 

/* Close the log file */
log close
